package com.mm.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.entity.MmGoods;
import com.mm.exception.BusinessException;
import com.mm.service.impl.MmGoodsServiceImpl;
import com.mm.enums.ResultCodeEnum;
import com.mm.utils.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 商品基本信息表 前端控制器
 * </p>
 *
 * @author wdx
 * @since 2020-09-17
 */
@Api(tags = "商品API")
@Validated
@RestController
@RequestMapping("/mm-goods")
public class MmGoodsController {

    @Autowired
    MmGoodsServiceImpl mmGoodsService;

    @ApiOperation(value = "分页列表")
    @GetMapping(value = "/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNo", value = "当前页", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页条数", defaultValue = "10", dataType = "int", paramType = "query")
    })
    @RequiresPermissions("goods:page")
    public ApiResult<MmGoods> page(@NotNull(message = "当前页不能为空") Integer pageNo, @NotNull(message = "页条数不能为空") Integer pageSize) {
        IPage<MmGoods> page = mmGoodsService.page(new Page<>(pageNo, pageSize));
        return ApiResult.success(page);
    }

    @ApiOperation(value = "保存")
    @GetMapping(value = "/save")
    public ApiResult save(@RequestBody MmGoods mGoods) {
        boolean save = mmGoodsService.save(mGoods);
        if (!save) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "详情")
    @GetMapping(value = "/detail")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    public ApiResult<MmGoods> detail(@NotBlank(message = "ID不能为空") Integer id) {
        MmGoods mGoods = mmGoodsService.getById(id);
        if (mGoods == null) {
            throw new BusinessException(ResultCodeEnum.NO_DATA);
        }
        return ApiResult.success();
    }


    @ApiOperation(value = "修改")
    @GetMapping(value = "/update")
    public ApiResult update(@RequestBody MmGoods mGoods) {
        boolean update = mmGoodsService.updateById(mGoods);
        if (!update) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    @GetMapping(value = "/del")
    public ApiResult del(@NotBlank(message = "ID不能为空") Integer id) {
        boolean del = mmGoodsService.removeById(id);
        if (!del) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    /**
     * 查询条件简化
     *
     * @return
     */
    public LambdaQueryWrapper<MmGoods> query() {
        return new LambdaQueryWrapper<MmGoods>();
    }

}
